package com.api.syh56.shengyunhui.service.impl;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.User;
import com.api.syh56.shengyunhui.entity.Waybill;
import com.api.syh56.shengyunhui.entity.WithDraw;
import com.api.syh56.shengyunhui.entity.vo.WAGParamVO;
import com.api.syh56.shengyunhui.entity.vo.WithDrawVO;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.mapper.WayBillMapper;
import com.api.syh56.shengyunhui.mapper.WithDrawMapper;
import com.api.syh56.shengyunhui.service.UserService;
import com.api.syh56.shengyunhui.service.WithDrawService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WithDrawServiceImpl extends ServiceImpl<WithDrawMapper, WithDraw> implements WithDrawService {

    @Autowired
    private UserService userService;

    @Override
    public void UserWithdraw(WithDraw withDraw) {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        User user = userService.getById(uid);

        //查询提现申请中的金额
        QueryWrapper<WithDraw> withDrawQueryWrapper = new QueryWrapper<>();
        withDrawQueryWrapper.lambda()
                .eq(WithDraw::getUid, uid)
                .eq(WithDraw::getState, Constants.WithdrawalStatus.IN_PROGRESS);
        List<WithDraw> list = list(withDrawQueryWrapper);
        BigDecimal IN_PROGRESS_amount = new BigDecimal(0);
        for (WithDraw item : list) {
//            if (item.getState().equals(Constants.WithdrawalStatus.IN_PROGRESS)) {
//                throw new BaseException("存在未支付的提现申请");
//            }
            IN_PROGRESS_amount = IN_PROGRESS_amount.add(item.getAmount());
        }

        //可提现余额
        BigDecimal withdrawAble = user.getBalanceAmt().subtract(IN_PROGRESS_amount);
        //本次提现金额
        BigDecimal amount = withDraw.getAmount();

        System.out.println(amount);
        System.out.println(user.getBalanceAmt());
        System.out.println(IN_PROGRESS_amount);
        if (withdrawAble.compareTo(amount) >= 0) {
            withDraw.setUid(uid);
            withDraw.setState(Constants.WithdrawalStatus.IN_PROGRESS);
            save(withDraw);
        }else {
            throw new BaseException("已有"+list.size()+"笔提现已在申请中，提现金额大于可提现余额");
        }
    }

    @Override
    public Page<WithDrawVO> WithdrawPageToWithdrawVOPage(Page<WithDraw> withdrawPage) {
        Page<WithDrawVO> WithdrawVOPage = new Page<>();
// 复制分页信息
        WithdrawVOPage.setCurrent(withdrawPage.getCurrent());
        WithdrawVOPage.setSize(withdrawPage.getSize());
//        WAGParamVOPage.setPages(waybillPage.getPages());
//        WAGParamVOPage.setTotal(waybillPage.getTotal());
// 转换数据列表方法1（Stream API）
        List<WithDrawVO> withDrawVOList = withdrawPage.getRecords().stream()
                .map(withDraw -> ConvertWithDrawToWithDrawVO(withDraw)) // 使用方法引用进行转换
                .filter(withDrawVO -> withDrawVO.getUser() != null)
                .collect(Collectors.toList());

        // 更新分页信息
        WithdrawVOPage.setPages((int) Math.ceil((double) withDrawVOList.size() / withdrawPage.getSize()));
        WithdrawVOPage.setTotal(withDrawVOList.size());
        WithdrawVOPage.setRecords(withDrawVOList);

        return WithdrawVOPage;
    }

    @Override
    public WithDrawVO ConvertWithDrawToWithDrawVO(WithDraw withDraw) {
        Long uid = withDraw.getUid();
        User user = userService.getById(uid);
        WithDrawVO withDrawVO = WithDrawVO.builder()
                .withdraw(withDraw)
                .user(user).build();
        return withDrawVO;
    }


    public void applyAmount(BigDecimal amount,User user) {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        user.setWithdrawAble(user.getWithdrawAble().subtract(amount));


    }
}
